package com.example.demo.service.imp;

import com.example.demo.contrller.data.ReturnData;
import com.example.demo.contrller.vo.RegisterUser;
import com.example.demo.dao.AdminDao;
import com.example.demo.dao.CircleDao;
import com.example.demo.dao.CommentDao;
import com.example.demo.dao.UserDao;
import com.example.demo.demos.*;
import com.example.demo.service.AdminService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImp implements AdminService {
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    AdminDao adminDao;

    @Override
    public ReturnData login(String user, String pwd) {
        List<Admin> users = adminDao.findByUsernameAndPassword(user, pwd);
        if(users.size()>0){//如果有，登陆成功
            HashMap<String, Object> map = new HashMap<>();
            map.put("token", com.example.demo.utils.TokenUtils.getToken(users.get(0).getUsername()));
            users.get(0).setPassword("");
            map.put("userInfo",users.get(0));

            User temp = new User();
            temp.setId(users.get(0).getId());
            temp.setUsername(users.get(0).getUsername());
            temp.setPassword(users.get(0).getPassword());
            temp.setAvatar(users.get(0).getAvatar());
            temp.setNickname(users.get(0).getNickname());

            redisTemplate.opsForValue().set(users.get(0).getUsername(),temp);//写进缓存
//            redisTemplate.opsForValue().get(users.get(0).getUsername());//读缓存
            return ReturnData.ok("登陆成功",map);
        }else {//查找不到对应的用户数据
            return ReturnData.error("登录失败");//返回空，此时要在前端提示登录失败
        }
    }
    @Resource
    UserDao userDao;

    /**
     *
     * 查找数据库获取圈子数据
     */
    @Override
    public ReturnData getUserList(int page, int pageSize,String nickname) {
        List<Map<String, Object>> list = userDao.getList((page - 1) *pageSize, pageSize,nickname);

        long num = userDao.count();//获取表里面所有数据的条数
        long total = 0;//页码数
        if (num %pageSize== 0) {
            total = num / pageSize;
        } else {
            total = num / pageSize + 1;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", list);
        return ReturnData.ok("成功", map);
    }

    @Override
    public ReturnData deleteUser(int id) {
        User user=userDao.findById(id).get();
        if(user==null){
            return ReturnData.error("没有这个用户");
        }
        userDao.delete(user);
        return  ReturnData.ok("删除成功");
    }

    @Override
    public ReturnData updateUserInfo(RegisterUser registerUser) {
        User user;
        if(registerUser.getId()==0){
            user =new User();
            if(!StringUtils.hasText(registerUser.getUsername())){
                return ReturnData.error("请输入用户名") ;
            }
            if(!StringUtils.hasText(registerUser.getPassword())){
                return ReturnData.error("请输入密码") ;
            }
            if(!StringUtils.hasText(registerUser.getEmail())){
                return ReturnData.error("请输入邮箱") ;
            }
            User userObj = new User();
            userObj.setUsername(registerUser.getUsername());
            userObj.setPassword(registerUser.getPassword());
            userObj.setEmail(registerUser.getEmail());
            userObj.setRegister_date(new Timestamp(System.currentTimeMillis()));
     }else {
             user=userDao.findById(registerUser.getId()).get();
        }

        if(!StringUtils.hasText(registerUser.getNickname())){
            return ReturnData.error("请输入昵称") ;
        }
        if(!StringUtils.hasText(registerUser.getPhone())){
            return ReturnData.error("请输入手机") ;
        }
        if(registerUser.getPhone().length()!=11){
            return ReturnData.error("手机格式有误") ;
        }
//        if(!StringUtils.hasText(registerUser.getSgin())){
//            return ReturnData.error("请输入签名") ;
//        }
        if(registerUser.getSex()==0){
            return ReturnData.error("请选择性别") ;
        }
        if(!StringUtils.hasText(registerUser.getAvatar())){
            return ReturnData.error("请上传头像") ;
        }
        user.setNickname(registerUser.getNickname());
        user.setPhone(registerUser.getPhone());
        user.setSgin(registerUser.getSgin());
        user.setSex(registerUser.getSex());
        user.setAvatar(registerUser.getAvatar());
        userDao.save(user);//改完存回数据库
        return ReturnData.ok("修改成功");
    }
    @Resource
    CircleDao circleDao;
    @Override
    public ReturnData getCircle(int page, int pageSize, String content) {

        List<Map<String, Object>> list = circleDao.getAdminList((page - 1) * pageSize, pageSize, content);

        long num = circleDao.count();//获取表里面所有数据的条数
        long total = 0;//页码数
        if (num % pageSize == 0) {
            total = num / pageSize;
        } else {
            total = num / pageSize + 1;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", list);
        return ReturnData.ok("成功", map);
    }

    /**
     * 删除圈子
     * @param id
     * @return
     */
    @Override
    public ReturnData deleteCircle(int id) {
        Circle circle=circleDao.findById(id).get();
        if (circle==null){
            return ReturnData.error("删除失败，没有对应的ID");
        }
        circleDao.delete(circle);
        return ReturnData.ok("删除失败");
    }
    @Resource
    CommentDao commentDao;
    @Override
    public ReturnData getComment(int page, int pageSize, int circleId) {
        List<Map<String, Object>> adminList = commentDao.getAdminList((page - 1) * pageSize, pageSize, circleId);
        int count = commentDao.getCountByCircleId(circleId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", count);
        map.put("list", adminList);
        return ReturnData.ok("成功", map);
    }

    /**
     * 根据id删除评论
     * @param id
     * @return
     */
    @Override
    public ReturnData deleteComment(int id) {
        Comment comment= commentDao.findById(id).get();
        if (comment==null){
            return ReturnData.error("删除失败，没有对应的ID");
        }
        commentDao.delete(comment);
        return ReturnData.ok("删除失败");
    }
}

